package merchant

import (
	"github.com/gin-gonic/gin"
	"organoid_public/global"
)

type handler struct {
	svc *service
}

func NewHandler() (*handler, error) {
	svc, err := NewService()
	if err != nil {
		return nil, err
	}
	return &handler{
		svc: svc,
	}, err
}

func (h *handler) detail(c *gin.Context) {
	var (
		params   DetailReq
		response = global.Response.New(c)
	)
	err := response.BindWithValidate(&params)
	if err != nil {
		return
	}
	info := h.svc.detail(params.Id)
	response.Success(info).ToJSON()
}

func (h *handler) upsert(c *gin.Context) {
	var (
		params   UpInsertReq
		response = global.Response.New(c)
	)

	err := response.BindWithValidate(&params)
	if err != nil {
		return
	}
	OpenAppId, _ := c.Get("OpenAppId")
	info := h.svc.upsert(&params, OpenAppId.(string))
	response.Success(info).ToJSON()
}

func (h *handler) bindStore(c *gin.Context) {
	var (
		params   BindStoreReq
		response = global.Response.New(c)
	)

	err := response.BindWithValidate(&params)
	if err != nil {
		return
	}
	OpenAppId, _ := c.Get("OpenAppId")
	params.OpenAppId = OpenAppId.(string)
	info := h.svc.bindStore(&params)
	response.Success(info).ToJSON()
}

func (h *handler) listPage(c *gin.Context) {

}
